home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / dos / flush.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  2KB  |  97 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: flush.c,v 1.3 1996/08/13 13:52:46 digulla Exp $
  4.     $Log: flush.c,v $
  5.     Revision 1.3  1996/08/13 13:52:46  digulla
  6.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  7.     Replaced __AROS_LA by __AROS_LHA
  8.  
  9.     Revision 1.2  1996/08/01 17:40:51  digulla
  10.     Added standard header for all files
  11.  
  12.     Desc:
  13.     Lang: english
  14. */
  15. #include <dos/dosextens.h>
  16. #include <clib/dos_protos.h>
  17.  
  18. /*****************************************************************************
  19.  
  20.     NAME */
  21.     #include <clib/dos_protos.h>
  22.  
  23.     __AROS_LH1(LONG, Flush,
  24.  
  25. /*  SYNOPSIS */
  26.     __AROS_LHA(BPTR, file, D1),
  27.  
  28. /*  LOCATION */
  29.     struct DosLibrary *, DOSBase, 60, Dos)
  30.  
  31. /*  FUNCTION
  32.     Flushes any pending writes on the file. If the file was used
  33.     for input and there is still some data to read it tries to
  34.     seek back to the expected position.
  35.  
  36.     INPUTS
  37.     file - filehandle
  38.  
  39.     RESULT
  40.     !=0 on success, 0 on error. IoErr() gives additional information
  41.     in that case.
  42.  
  43.     NOTES
  44.  
  45.     EXAMPLE
  46.  
  47.     BUGS
  48.  
  49.     SEE ALSO
  50.  
  51.     INTERNALS
  52.  
  53.     HISTORY
  54.     29-10-95    digulla automatically created from
  55.                 dos_lib.fd and clib/dos_protos.h
  56.  
  57. *****************************************************************************/
  58. {
  59.     __AROS_FUNC_INIT
  60.     __AROS_BASE_EXT_DECL(struct DosLibrary *,DOSBase)
  61.  
  62.     /* Get pointer to filehandle */
  63.     struct FileHandle *fh=(struct FileHandle *)BADDR(file);
  64.     long success=1;
  65.  
  66.     /* If the file is in write mode... */
  67.     if(fh->fh_Flags&FHF_WRITE)
  68.     {
  69.     UBYTE *pos;
  70.     /* Unset write flag */
  71.     fh->fh_Flags&=~FHF_WRITE;
  72.  
  73.     /* Write the data. (In many pieces if the first one isn't enough). */
  74.     pos=fh->fh_Buf;
  75.         while(pos!=fh->fh_Pos)
  76.         {
  77.             LONG size;
  78.         size=Write(file,pos,fh->fh_Pos-pos);
  79.  
  80.         /* An error happened? No success. */
  81.         if(size<0)
  82.         {
  83.             success=0;
  84.         break;
  85.         }
  86.         pos+=size;
  87.     }
  88.     }else if(fh->fh_Pos<fh->fh_End)
  89.         /* Read mode. Try to seek back to the current position. */
  90.         if(Seek(file,fh->fh_Pos-fh->fh_End,OFFSET_CURRENT)<0)
  91.             success=0;
  92.     /* Reinit the buffer and return. */
  93.     fh->fh_Pos=fh->fh_End=fh->fh_Buf;
  94.     return success;
  95.     __AROS_FUNC_EXIT
  96. } /* Flush */
  97.